﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using ClassLibrary;

namespace WebApplication2
{
    public partial class Admin : System.Web.UI.Page
    {
        connectionClass conn = new connectionClass();
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            bool success;
            ListViewItem item = (ListViewItem)ListView1.Items[e.ItemIndex];
            Guid roleId = Guid.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
            TextBox appId = (TextBox)item.FindControl("ApplicationIdTextBox");
            TextBox roleName = (TextBox)item.FindControl("RoleNameTextBox");
            TextBox desc = (TextBox)item.FindControl("DescriptionTextBox");

            try{
                conn.getConnectionAdmin();
                try{
                    success = conn.updateRoles(roleId, Guid.Parse(appId.Text.ToString()), roleName.Text.ToString(), desc.Text.ToString());
                    conn.closeConnection();
                    if (success)
                    {
                        Response.Write("<script>alert('De tabel is bijgewerkt');</script>");
                    }
                    ListView1.EditIndex = -1;
                }
                catch (Exception ex){
                    Logs.Log(ex.Message + " " + ex.StackTrace);
                    Response.Write("~/Oops.aspx");
                }               
            }
            catch (Exception ex){
                Logs.Log(ex.Message + " " + ex.StackTrace);
                Response.Write("~/Oops.aspx");
            }
         }

        protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            ListView1.EditIndex = e.NewEditIndex;
        }

        protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            e.Cancel = true;
            ListView1.EditIndex = -1;
        }

        protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            bool success;
            ListViewItem item = (ListViewItem)ListView1.Items[e.ItemIndex];
            Guid roleId = Guid.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
            try
            {
                conn.getConnectionAdmin();
                try
                {
                    success = conn.deleteRoles(roleId);
                    conn.closeConnection();
                    if (success)
                    {
                        Response.Write("<script>alert('Het record is verwijderd');</script>");
                    }
                    ListView1.EditIndex = -1;
                }
                catch (Exception ex)
                {
                    Logs.Log(ex.Message + " " + ex.StackTrace);
                    Response.Write("~/Oops.aspx");
                }
            }
            catch (Exception ex)
            {
                Logs.Log(ex.Message + " " + ex.StackTrace);
                Response.Write("~/Oops.aspx");
            }
        }
    }
}